<?php
/**
 * Created by PhpStorm.
 * User: lofate
 * Date: 2019/12/19
 * Time: 09:30
 */

namespace app\adminapi\controller\v1\marketing;

use app\adminapi\controller\AuthController;
use app\Request;
use app\services\activity\StoreCombinationServices;
use app\services\activity\StorePinkServices;
use app\services\order\StoreOrderServices;
use app\services\user\UserServices;
use think\facade\App;
use think\facade\Db;


/**
 * 拼团管理
 * Class StoreCombination
 * @package app\admin\controller\store
 */
class StoreCombination extends AuthController {
    public function __construct(App $app, StoreCombinationServices $services) {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 拼团列表
     * @return mixed
     */
    public function index() {
        $where = $this->request->getMore([
            ['is_show', ''],
            ['store_name', '']
        ]);
        $where['is_del'] = 0;
        $list = $this->services->systemPage($where);
        return $this->success($list);
    }

    /**
     * 拼团统计
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function statistics() {
        /** @var StorePinkServices $storePinkServices */
        $storePinkServices = app()->make(StorePinkServices::class);
        $info = $storePinkServices->getStatistics();
        return $this->success($info);
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     */
    public function read($id) {
        $info = $info = $this->services->getInfo((int)$id);
        return $this->success(compact('info'));
    }

    /**
     * 保存新建的资源
     * @param int $id
     */
    public function save($id = 0) {
        $data = $this->request->postMore([
            [['product_id', 'd'], 0],
            [['title', 's'], ''],
            [['info', 's'], ''],
            [['unit_name', 's'], ''],
            ['image', ''],
            ['images', []],
            ['section_time', []],
            [['is_host', 'd'], 0],
            [['is_show', 'd'], 0],
            [['num', 'd'], 0],
            [['temp_id', 'd'], 0],
            [['effective_time', 'd'], 0],
            [['people', 'd'], 0],
            [['description', 's'], ''],
            ['attrs', []],
            ['items', []],
            ['num', 1],
            ['sort', 0]
        ]);
        $this->validate($data, \app\adminapi\validate\marketing\StoreCombinationValidate::class, 'save');
        $this->services->saveData($id, $data);
        return $this->success('保存成功');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id) {
        $this->services->update($id, ['is_del' => 1]);
        return $this->success('删除成功!');
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status) {
        $this->services->update($id, ['is_show' => $status]);
        return $this->success($status == 0 ? '关闭成功' : '开启成功');
    }

    /**拼团列表
     * @return mixed
     */
    public function combine_list() {
        $where = $this->request->getMore([
            ['status', ''],
            ['data', '', '', 'time'],
        ]);
        /** @var StorePinkServices $storePinkServices */
        $storePinkServices = app()->make(StorePinkServices::class);
        $list = $storePinkServices->systemPage($where);
        return $this->success($list);
    }

    /**拼团人列表
     * @return mixed
     */
    public function order_pink($id) {
        /** @var StorePinkServices $storePinkServices */
        $storePinkServices = app()->make(StorePinkServices::class);
        $list = $storePinkServices->getPinkMember($id);
        return $this->success(compact('list'));
    }

    /**
     * 获取拼团返利设置
     */
    public function get_setting() {
        $setting = Db::table('eb_combination_setting')->select()->toArray();
        $config = array();
        foreach ($setting as $key => $value) {
            $config[$value['name']] = $value['value'];
        }
        $return['status'] = 200;
        $return['config'] = $config;
        return json($return);
    }

    /**
     * 拼团返利设置
     */
    public function set_setting() {
        $data = $this->request->getMore([
            ['pink4_winning', 0],
            ['pink4_rebate', 0],
            ['pink4_time', 0],
            ['pink5_winning', 0],
            ['pink5_rebate', 0],
            ['pink5_time', 0],
            ['pink6_winning', 0],
            ['pink6_rebate', 0],
            ['pink6_time', 0],
            ['pink7_winning', 0],
            ['pink7_rebate', 0],
            ['pink7_time', 0],
            ['pt_start', 0],
            ['pt_end', 0],
            ['parent4_percent', 0],
            ['parent4_time', date('Y-m-d')],
            ['parent5_percent', 0],
            ['parent5_time', date('Y-m-d')],
            ['parent6_percent', 0],
            ['parent6_time', date('Y-m-d')],
            ['parent7_percent', 0],
            ['parent7_time', date('Y-m-d')],
            ['win4_point_num', 0],//中团多少次获得参团点
            ['win4_point_times', 0],//中团多少次获得多少倍参团点
            ['win5_point_num', 0],//中团多少次获得参团点
            ['win5_point_times', 0],//中团多少次获得多少倍参团点
            ['win6_point_num', 0],//中团多少次获得参团点
            ['win6_point_times', 0],//中团多少次获得多少倍参团点
            ['win7_point_num', 0],//中团多少次获得参团点
            ['win7_point_times', 0],//中团多少次获得多少倍参团点
        ]);
        foreach($data as $key=>$value){
            Db::table('eb_combination_setting')->where('name', $key)->update([
                'value' => $value
            ]);
        }
        $return['status'] = 200;
        $return['msg'] = '修改成功';
        return json($return);
    }

    /**
     * 拉新配置接口
     * @return \think\response\Json
     */
    public function setNewConfig() {
        $data = $this->request->getMore([
            ['new_winning', 0],
            ['new_winning_time', date('Y-m-d')],
            ['new_combine_percent', 0],
            ['new_combine_time', date('Y-m-d')],
        ]);
        foreach($data as $key=>$value){
            Db::table('eb_combination_setting')->where('name', $key)->update([
                'value' => $value
            ]);
        }
        $return['status'] = 200;
        $return['msg'] = '修改成功';
        return json($return);
    }

    /**
     * 获取拉新配置
     * @return \think\response\Json
     */
    public function getNewConfig() {
        $setting = Db::table('eb_combination_setting')->field('name,value')->select()->toArray();
        $config = [];
        foreach ($setting as $key => $value) {
            $config[$value['name']] = $value['value'];
        }
        $return['status'] = 200;
        $return['config'] = $config;
        return json($return);
    }

    public function getRankConfig(){
        $setting = Db::table('eb_rank_integral_config')->where(1,1)->find();
        return app('json')->success($setting);
    }

    public function setRankConfig(){
        $data = $this->request->getMore([
            ['id', 0],
            ['rank_desc', ''],
            ['limit_integral', 0],
            ['max_times', 0],
            ['change_percent', 0],
            ['status', 1],
            ['open_in_rank_money', 1],
            ['open_out_rank_money', 1],
            ['out_day_rank_money', 1],
            ['new_user_num', 1],
            ['join_pink_num', 1],
        ]);
        if(!$data['id']){
            return app('json')->fail('修改ID不能为空');
        }
        if(!$data['rank_desc']){
            return app('json')->fail('排行榜积分订单描述');
        }
        if(!$data['limit_integral']){
            return app('json')->fail('每单消耗积分不能为空');
        }
//        if(!$data['max_times']){
//            return app('json')->fail('当天最大兑换次数不能为空');
//        }
        if(!$data['change_percent']){
            return app('json')->fail('兑换比例不能为空');
        }
        $saveInfo=[
            'rank_desc'=>$data['rank_desc'],
            'limit_integral'=>$data['limit_integral'],
            'max_times'=>$data['max_times'],
            'change_percent'=>$data['change_percent'],
            'status'=>$data['status'],
            'open_in_rank_money'=>$data['open_in_rank_money'],
            'open_out_rank_money'=>$data['open_out_rank_money'],
            'out_day_rank_money'=>$data['out_day_rank_money'],
            'new_user_num'=>$data['new_user_num'],
            'join_pink_num'=>$data['join_pink_num'],
        ];
        $rs=Db::table('eb_rank_integral_config')->where(['id'=>$data['id']])->update($saveInfo);
        if($rs){
            return app('json')->success('操作成功');
        }
        return app('json')->fail('操作失败');
    }
}
